<?
class Control
{
	var $name;
	var $value;
	var $title;
	var $table;
	
	var $visible=true;
	
	function Control($name,$title,$value="")
	{
		$this->title=$title;
		$this->name=$name;
		$this->value=$value;
	}
	
	function Render()
	{
		return;
	}
}



class Hidden extends Control
{
	function Hidden($name,$value="")
	{
		$this->name=$name;
		$this->value=$value;
	}
	
	function Render()
	{
		return '<input type="hidden" name="'.$this->name.'" value="'.$this->value.'" id="input_'.$this->name.'">';
	}
}

class Label extends Control
{
	function Render()
	{
		return $this->value;
	}
}



class Str extends Control
{
	function Render()
	{
		return '<input type="Text" name="'.$this->name.'" value="'.htmlspecialchars($this->value).'" id="input_'.$this->name.'">';
	}
}






class Textarea extends Control
{
	function Render()
	{
		$this->value = str_replace('<#','<!#',$this->value);
		$this->value = str_replace('images','!images!',$this->value);
		return "<div class='resizable-textarea'  unselectable=on><span><textarea name='".$this->name."' id='area_g".$this->name."'>".$this->value."</textarea><div unselectable=on class='grippie' id='g".$this->name."'></div></span><p></p>";
	}
}

class TextareaSite extends Control
{
	function Render()
	{
		return '<Textarea name="'.$this->name.'" id="input_'.$this->name.'">'.$this->value.'</textarea>';
	}
}





class Select extends Control
{
	var $source;
	
	function Select($name,$title,$source=Array(),$value="")
	{
		$this->title=$title;
		$this->name=$name;
		$this->value=$value;
		$this->source=$source;
	}
	
	function Render()
	{
		$ret = '<select name="'.$this->name.'" id="input_'.$this->name.'">';
		if(is_array($this->source))
		foreach($this->source as $k=>$v)
		{
			if($k==$this->value) $chk="selected";
			else $chk="";
			
			$ret.='<option '.$chk.' value="'.$k.'">'.$v;
		}
		$ret.='</select>';
		return $ret;
	}
}


class CheckboxGroup extends Control
{
	var $source;
	var $keyname;
	var $keyvalue;
	var $secondaryname;
	
	var $skipSql=true;
	var $handleAdd=true;
	var $handleUpdate=true;
	var $handleDelete=true;
	
	function CheckboxGroup($name,$title,$source,$value,$keyname,$secondaryname,$keyvalue)
	{
		$this->title=$title;
		$this->name=$name;
		$this->value=$value;
		$this->source=$source;
		$this->keyname=$keyname;
		$this->secondaryname=$secondaryname;
		$this->keyvalue=$keyvalue;
	}
	
	function Render()
	{
		if(is_array($this->source))
		foreach($this->source as $k=>$v)
		{
			if(array_key_exists($k,$this->value))$chk="checked";
			else $chk="";
			
			$ret.='<input type="Checkbox" name="'.$this->name.$k.'" '.$chk.' value="1" class=checkbox>'.$v.'<br>';
		}
		return $ret;
	}
	
	function OnUpdate($id)
	{
		$this->OnDelete($id);
		
		foreach($this->source as $k=>$v)
		{
			if( $_REQUEST[$this->name.$k] ) 
			{
				DBInsert($this->name,Array($this->keyname,$this->secondaryname), Array($this->keyname=>$this->keyvalue,$this->secondaryname=>$k));
				//mysql_query("insert into {$this->name} ($this->keyname,$this->secondaryname) values ('{$this->keyvalue}','{$k}')");
			}
		}
	}
	
	function OnAdd($id)
	{
		$this->OnUpdate($id);
	}
	
	function OnDelete($id)
	{
		DBDelete($this->name,"{$this->keyname}='{$this->keyvalue}'");
		//mysql_query("delete from {$this->name} where {$this->keyname}='{$this->keyvalue}'");
	}
}


class CheckboxGroupPlain extends Control
{
// представление значений в строку. разделитель скобочки (x)
	var $source;
	
	var $skipSql=true;
	var $handleAdd=true;
	var $handleUpdate=true;
	
	function CheckboxGroupPlain($name,$title,$source)
	{
		$this->title=$title;
		$this->name=$name;
		$this->source=$source;
	}
	
	function Render()
	{
		if(is_array($this->source))
		foreach($this->source as $key=>$val)
		{
			if( is_int(strpos($this->value,'('.$key.')')) )$chk="checked";
			else $chk="";
			
			$ret.='<input type="Checkbox" name="'.$this->name.$key.'" '.$chk.' value="1" class=checkbox>'.$val.'<br>';
		}
		return $ret.'<br>';
	}
	
	function OnUpdate($id)
	{
		foreach($this->source as $k=>$v)
		{
			if( $_REQUEST[$this->name.$k] ) $newval.="(".$k.")";
		}
		DBUpdate($this->table, Array($this->name),Array($this->name=>$newval,'id'=>$id));
		//mysql_query("update $this->table set $this->name='$newval' where id=$id");
	}
	
	function OnAdd($id)
	{
		$this->OnUpdate($id);
	}
}



class RadioGroup extends Control
{
	var $source;
	
	function RadioGroup($name,$title,$source)
	{
		$this->title=$title;
		$this->name=$name;
		$this->source=$source;
	}
	
	function Render()
	{
		if(is_array($this->source))
		foreach($this->source as $key=>$val)
		{
			if( $this->value==$key )$chk="checked";
			else $chk="";
			
			$ret.='<input type="Radio" name="'.$this->name.'" '.$chk.' value="'.$key.'" class=checkbox>'.$val.'<br>';
		}
		return $ret.'<br>';
	}
}




class Data extends Control
{
	function Render()
	{
		if($this->value=="") $this->value=time();
		$name=$this->name;
		$day=date("d",$this->value);
		$month=date("m",$this->value);
		$year=date("Y",$this->value);
		
		return '<input name="'.$name.'_day" type="text" value="'.$day.'" maxlength="2" class="small">
		        -
		        <input name="'.$name.'_month" type="text" value="'.$month.'" maxlength="2" class="small">
		        -
		        <input name="'.$name.'_year" type="text" value="'.$year.'" maxlength="4" class="small">
				 (день-месяц-год)';
	}
}


class DataExt extends Control
{
	function Render()
	{
		if($this->value=="") $this->value=time();
		$name=$this->name;
		$day=date("d",$this->value);
		$month=date("m",$this->value);
		$year=date("Y",$this->value);
		$hour=date("H",$this->value);
		$minute=date("i",$this->value);
		
		return '<input name="'.$name.'_day" type="text" value="'.$day.'" maxlength="2" class="small">
		        -
		        <input name="'.$name.'_month" type="text" value="'.$month.'" maxlength="2" class="small">
		        -
		        <input name="'.$name.'_year" type="text" value="'.$year.'" maxlength="4" class="small">
		  			&nbsp;
						<input name="'.$name.'_hour" type="text" value="'.$hour.'" maxlength="2" class="small">
						:
						<input name="'.$name.'_minute" type="text" value="'.$minute.'" maxlength="2" class="small">
				 (день-месяц-год часы:минуты)';
	}
}







class Checkbox extends Control
{
	function Render()
	{
		if($this->value=="1") $checked="checked";
		
		return '<input name="'.$this->name.'" type="checkbox" value="1" '.$checked.' class=checkbox  id="input_'.$this->name.'">';
	}
}


